home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Resources / Developers / XAMPP 1.5.4 / Windows installer / xampp-win32-1.5.4-installer.exe / xampp / php / pear / Net / DNSBL.php < prev    next >
Encoding:
PHP Script  |  2006-04-07  |  5.7 KB  |  212 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4.  
  5. /**
  6.  * PEAR::Net_DNSBL
  7.  *
  8.  * This class acts as interface to generic Realtime Blocking Lists
  9.  * (RBL)
  10.  *
  11.  * PHP versions 4 and 5
  12.  *
  13.  * LICENSE: This source file is subject to version 3.0 of the PHP license
  14.  * that is available through the world-wide-web at the following URI:
  15.  * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  16.  * the PHP License and are unable to obtain it through the web, please
  17.  * send a note to license@php.net so we can mail you a copy immediately.
  18.  *
  19.  * Net_RBL looks up an supplied host if it's listed in 1-n supplied
  20.  * Blacklists
  21.  *
  22.  * @category   Net
  23.  * @package    DNSBL
  24.  * @author     Sebastian Nohn <sebastian@nohn.net>
  25.  * @author     Ammar Ibrahim <fixxme@fixme.com>
  26.  * @copyright  2004-2006 Sebastian Nohn <sebastian@nohn.net>
  27.  * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  28.  * @version    CVS: $Id: DNSBL.php,v 1.11 2006/03/11 14:47:42 nohn Exp $
  29.  * @link       http://pear.php.net/package/Net_DNSBL
  30.  * @see        Net_DNS
  31.  * @since      File available since Release 1.0.0
  32.  */
  33.  
  34. require_once 'Net/CheckIP.php';
  35.  
  36. class Net_DNSBL {
  37.  
  38.     /**     
  39.      * Array of blacklists.
  40.      *
  41.      * Must have one or more elements.
  42.      *
  43.      * @var    array
  44.      * @access protected
  45.      */
  46.     var $blacklists = array('sbl-xbl.spamhaus.org',
  47.                             'bl.spamcop.net');
  48.  
  49.     /**     
  50.      * Array of Results
  51.      *
  52.      * @var    array
  53.      * @access protected
  54.      */
  55.     var $results    = array();
  56.  
  57.     /**
  58.      * Set the blacklist to a desired blacklist.
  59.      *
  60.      * @param  array Array of blacklists to use. May contain only one element.
  61.      * @access public
  62.      * @return bool true if the operation was successful
  63.      */
  64.     function setBlacklists($blacklists)
  65.     {
  66.         if (is_array($blacklists)) {
  67.             $this->blacklists = $blacklists;
  68.             return true;
  69.         } else {
  70.             return false;
  71.         } // if
  72.     } // function
  73.  
  74.     /**
  75.      * Get the blacklists.
  76.      *
  77.      * @access public
  78.      * @return array Currently set blacklists.
  79.      */
  80.     function getBlacklists()
  81.     {
  82.         return $this->blacklists;
  83.     }
  84.  
  85.     /** 
  86.      * Returns Blacklist and Reply from the Blacklist, a host is listed in.
  87.      *
  88.      * @param  string Host to check
  89.      * @access public
  90.      * @return array result. $result['dnsbl'] contains DNSBL,
  91.      *               $result['record'] contains returned DNS record.
  92.      */
  93.     function getDetails($host)
  94.     {
  95.         if (isset($this->results[$host]['dnsbl'])) {
  96.             return $this->results[$host];
  97.         } else {
  98.             return false;
  99.         }
  100.     } // function
  101.  
  102.     /**
  103.      * Returns Blacklist, host is listed in.
  104.      *
  105.      * @param  string Host to check
  106.      * @access public
  107.      * @return bl, a host is listed in or false
  108.      */
  109.     function getListingBl($host)
  110.     {
  111.         if (isset($this->results[$host]['dnsbl'])) {
  112.             return $this->results[$host]['dnsbl'];
  113.         } else {
  114.             return false;
  115.         }
  116.     } // function
  117.  
  118.     /**
  119.      * Returns result, when a host is listed.
  120.      *
  121.      * @param  string Host to check
  122.      * @access public
  123.      * @return bl, a host is listed in or false
  124.      */
  125.     function getListingRecord($host)
  126.     {
  127.         if (isset($this->results[$host]['record'])) {
  128.             return $this->results[$host]['record'];
  129.         } else {
  130.             return false;
  131.         }
  132.     } // function
  133.  
  134.  
  135.     /** 
  136.      * Checks if the supplied Host is listed in one or more of the
  137.      * RBLs.
  138.      *
  139.      * @param  string Host to check for being listed.
  140.      * @access public
  141.      * @return boolean true if the checked host is listed in a blacklist.
  142.      */
  143.     function isListed($host)
  144.     {
  145.         
  146.         $isListed = false;
  147.         
  148.         foreach ($this->blacklists as $blacklist) {
  149.             $result = gethostbyname($this->getHostForLookup($host, $blacklist));
  150.             if ($result != $this->getHostForLookup($host, $blacklist)) { 
  151.                 $isListed = true;
  152.                 $this->results[$host]['dnsbl']  = $blacklist;
  153.                 $this->results[$host]['record'] = $result;
  154.                 // $results[$host]['txt']    = 'FIXXME';
  155.                 //if the Host was listed we don't need to check other RBLs,
  156.                 break;
  157.                 
  158.             } // if
  159.         } // foreach
  160.         
  161.         return $isListed;
  162.     } // function
  163.  
  164.     /** 
  165.      * Get host to lookup. Lookup a host if neccessary and get the
  166.      * complete FQDN to lookup.
  167.      *
  168.      * @param  string Host OR IP to use for building the lookup.
  169.      * @param  string Blacklist to use for building the lookup.
  170.      * @access protected
  171.      * @return string Ready to use host to lookup
  172.      */    
  173.     function getHostForLookup($host, $blacklist) 
  174.     {
  175.         // Currently only works for v4 addresses.
  176.         if (!Net_CheckIP::check_ip($host)) {
  177.             $ip = gethostbyname($host);
  178.         } else {
  179.             $ip = $host;
  180.         }
  181.  
  182.         return $this->buildLookUpHost($ip, $blacklist);
  183.     } // function
  184.  
  185.     /**
  186.      * Build the host to lookup from an IP.
  187.      *
  188.      * @param  string IP to use for building the lookup.
  189.      * @param  string Blacklist to use for building the lookup.
  190.      * @access protected
  191.      * @return string Ready to use host to lookup
  192.      */    
  193.     function buildLookUpHost($ip, $blacklist)
  194.     {
  195.         return $this->reverseIp($ip).'.'.$blacklist;        
  196.     } // function
  197.  
  198.     /**
  199.      * Reverse the order of an IP. 127.0.0.1 -> 1.0.0.127. Currently
  200.      * only works for v4-adresses
  201.      *
  202.      * @param  string IP to reverse.
  203.      * @access protected
  204.      * @return string Reversed IP
  205.      */    
  206.     function reverseIp($ip) 
  207.     {        
  208.         return implode('.', array_reverse(explode('.', $ip)));        
  209.     } // function
  210.  
  211. } // class
  212. ?>